﻿using System.ComponentModel.DataAnnotations;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Foundation.Core;
using Microsoft.AspNetCore.Mvc;

namespace Dyao.Book.Web.Controllers
{
    public class BookController : ApiV1BaseController
    {
        private readonly IBookCatalogueApplication _bookCatalogueApp;

        private readonly IBookContentApplication _bookContentApp;

        private readonly IBookshelfApplication _bookshelfApp;

        private readonly IBookApplication _bookApp;

        public BookController(IBookCatalogueApplication bookCatalogueApp,
            IBookContentApplication bookContentApp,
            IBookshelfApplication bookshelfApp,
            IBookApplication bookApp)
        {
            _bookCatalogueApp = bookCatalogueApp;
            _bookContentApp = bookContentApp;
            _bookshelfApp = bookshelfApp;
            _bookApp = bookApp;
        }

        /// <summary>
        /// 获取书籍信息
        /// </summary>
        /// <param name="bookId">书籍编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<BookDto> GetBookAsync([Required(ErrorMessage = "书籍编号不能为空")] string bookId, CancellationToken cancellationToken)
        {
            var result = await _bookApp.GetBookAsync(bookId, cancellationToken);
            return result;
        }

        /// <summary>
        /// 获取书架信息
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<List<UserBookshelfDto>> GetBookshelfAsync(CancellationToken cancellationToken)
        {
            var result = await _bookshelfApp.GetUserBookshelfAsync(cancellationToken);
            return result;
        }

        /// <summary>
        /// 获取目录
        /// </summary>
        /// <param name="bookId">书籍编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<BookChapterDto> GetChaptersAsync([Required(ErrorMessage = "书籍编号不能为空")] string bookId, CancellationToken cancellationToken)
        {
            var result = await _bookCatalogueApp.GetListByBookIdAsync(bookId, cancellationToken);
            return result;
        }

        /// <summary>
        /// 获取正文内容
        /// </summary>
        /// <param name="chapterId">章节编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<List<BookContentDto>> GetContentsAsync([Required(ErrorMessage = "章节编号不能为空")] string chapterId, CancellationToken cancellationToken)
        {
            var result = await _bookContentApp.GetListByChapterIdAsync(chapterId, cancellationToken);
            return result;
        }

        /// <summary>
        /// 修改最后阅读时间
        /// </summary>
        /// <param name="dto">修改对象</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<bool> ModifyLastReadingChapterAsync(ModifyBookshelfLastReadingChapterDto dto, CancellationToken cancellationToken)
        {
            var result = await _bookshelfApp.ModifyLastReadingChapterAsync(dto, cancellationToken);
            return result;
        }

        /// <summary>
        /// 匹配书源
        /// </summary>
        /// <param name="bookId">书籍编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<bool> MatchChapterSourceAsync(string bookId, CancellationToken cancellationToken)
        {
            var result = await _bookCatalogueApp.MatchChapterSourceAsync(bookId, cancellationToken);
            return result;
        }

        /// <summary>
        /// 匹配书源
        /// </summary>
        /// <param name="bookId">书籍编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<bool> CrawlContentAsync(string bookId, CancellationToken cancellationToken)
        {
            var result = await _bookContentApp.CrawlContentAsync(bookId, cancellationToken);
            return result;
        }

        /// <summary>
        /// 获取书籍下拉选项
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<List<SelectItem>> GetBookSelectItemsAsync(CancellationToken cancellationToken)
        {
            var result = await _bookApp.GetBookSelectItemsAsync(cancellationToken);
            return result;
        }
    }
}
